import { _decorator, tween, v2, v3, Vec2 } from 'cc';
import { YangLeGeYang_GameData } from "../Data/YangLeGeYang_GameData";
import { YangLeGeYang_CardGameState } from "../Data/YangLeGeYang_GameEnum";
import YangLeGeYang_CardManager from "./YangLeGeYang_CardManager";
import YangLeGeYang_OperatePanel from "../UI/YangLeGeYang_OperatePanel";
import { ArrayUtils } from "../../../../Script/Utils/ArrayUtils";
import CommonFunction from "../../../../Script/Utils/CommonFunction";
import YangLeGeYang_LevelManager from "./YangLeGeYang_LevelManager";
import YangLeGeYang_Card from "../Game/YangLeGeYang_Card";

const { ccclass, property } = _decorator;

@ccclass('YangLeGeYangGameFunction')
export default class YangLeGeYang_GameFunction {
        //    // 刷新当前卡片是否被覆盖
        public static GetCardCover(itemdata: any, dataList: any[]) {
                let isCover: boolean = false;

                for (var i_2 = 0; i_2 <= dataList.length - 1; i_2++) {
                        const itemdata_up: any = dataList[i_2];
                        const pos_1 = itemdata.card_pos;
                        const pos_2 = itemdata_up.card_pos;
                        // if (!itemdata_up.isOperate && itemdata_up.card_layer > itemdata.card_layer) {
                        // itemdata.cardGameState = CardGameState.空地;

                        if (itemdata_up.cardGameState === YangLeGeYang_CardGameState.游戏场景 && itemdata_up.card_layer > itemdata.card_layer) {
                                // let t1: boolean = pos_1.x === pos_2.x && Math.abs(pos_1.y - pos_2.y) < 1;
                                // let t2: boolean = pos_1.y === pos_2.y && Math.abs(pos_1.x - pos_2.x) < 1;
                                // let t3: boolean = Math.pow(pos_1.x - pos_2.x, 2) + Math.pow((pos_1.y - pos_2.y), 2) < 2;
                                // if (t1 || t2 || t3) {
                                // if ((pos_1.x === pos_2.x && Math.abs(pos_1.y - pos_2.y) < 1) ||
                                //     (pos_1.y === pos_2.y && Math.abs(pos_1.x - pos_2.x) < 1) ||
                                //     (Math.pow(pos_1.x - pos_2.x, 2) + Math.pow((pos_1.y - pos_2.y), 2) < 2)) {
                                const isBanana = YangLeGeYang_GameFunction.IsBanana(pos_1, pos_2);
                                if (isBanana) {
                                        return true;
                                        // isCover = true;
                                        // break;
                                }
                                // }
                        }
                }

                return isCover;
        }
        //    // 刷新所有卡片覆盖
        public static RefreshAllCardCover(dataList: any[]) {
                for (var i = 0; i <= dataList.length - 1; i++) {
                        const itemdata: any = dataList[i];
                        let isCover = YangLeGeYang_GameFunction.GetCardCover(itemdata, dataList);
                        let card: YangLeGeYang_Card = YangLeGeYang_CardManager.Instance.CardList[i];
                        card.isCover = isCover;
                        card.Refresh();
                }
        }
        //    // 刷新单张卡牌附近九宫格卡片覆盖
        public static Refresh9CardCover(dataList: any[], _list_index, _card_layer, _card_pos) {
                // console.log(`card_layer,x,y)`, _card_layer, _card_pos._x, _card_pos._y);
                // console.log(`dataList`, dataList);
                // let tempList = [];
                // const itemdata: any = dataList[_list_index];
                for (var i = 0; i <= dataList.length - 1; i++) {
                        const itemdata: any = dataList[i];
                        // 1.4142135623730951
                        // if (itemdata.card_layer < _card_layer && Vec2.distance(_card_pos, itemdata.card_pos) < 1.42) {

                        // let t1: boolean = _card_pos.x === itemdata.card_pos.x && Math.abs(_card_pos.y - itemdata.card_pos.y) < 1;
                        // let t2: boolean = _card_pos.y === itemdata.card_pos.y && Math.abs(_card_pos.x - itemdata.card_pos.x) < 1;
                        // let t3: boolean = Math.pow(_card_pos.x - itemdata.card_pos.x, 2) + Math.pow(_card_pos.y - itemdata.card_pos.y, 2) < 2;

                        if (itemdata.card_layer < _card_layer
                                // && (_card_pos.x === itemdata.card_pos.x && Math.abs(_card_pos.y - itemdata.card_pos.y) < 1) ||
                                // (_card_pos.y === itemdata.card_pos.y && Math.abs(_card_pos.x - itemdata.card_pos.x) < 1) ||
                                // (Math.pow(_card_pos.x - itemdata.card_pos.x, 2) + Math.pow(_card_pos.y - itemdata.card_pos.y, 2) < 2)
                        ) {
                                // if (Math.pow(itemdata_up.card_pos.x - itemdata.card_pos.x, 2) + Math.pow((itemdata_up.card_pos.y - itemdata.card_pos.y), 2) < 2) {
                                // console.log(`itemdata`, itemdata.card_layer, itemdata.card_pos, itemdata);
                                let isCover = YangLeGeYang_GameFunction.GetCardCover(itemdata, dataList);
                                // tempList.push(itemdata);
                                let card: YangLeGeYang_Card = YangLeGeYang_CardManager.Instance.CardList[i];

                                card.isCover = isCover;
                                card.Refresh();
                        }
                }

                // console.log(`tempList.length`, tempList.length, tempList);

                // for (var i = 0; i <= tempList.length - 1; i++) {
                //     const itemdata: any = tempList[i];

                //     // console.log(`itemdata`, itemdata.card_layer, itemdata.card_pos, itemdata);

                //     let isCover = GameFunction.GetCardCover(itemdata, dataList);
                //     // let isCover = GameFunction.GetCardCover(itemdata, tempList);


                //     let card: Card = CardManager.Instance.CardList[itemdata.list_index];

                //     card.isCover = isCover;
                //     card.Refresh();
                // }

        }
        //    // // 是否相交
        //    // public static IsBanana(pos_1: Vec2, pos_2: Vec2) {
        //    //     // let isBanana: boolean = false;
        //    //     const w = 0.99;
        //    //     const rect_1 = new Rect(pos_1.x, pos_1.y, w, w);
        //    //     const rect_2 = new Rect(pos_2.x, pos_2.y, w, w);
        //    //     if (rect_1.intersects(rect_2)) {
        //    //         // isBanana = true;
        //    //         return true;
        //    //     }
        //    //     return false;
        //    //     // return isBanana;
        //    // }
        public static IsBanana(pos_1: Vec2, pos_2: Vec2): boolean {
                if (Math.abs(pos_1.x - pos_2.x) < 1 && Math.abs(pos_1.y - pos_2.y) < 1) return true;
                return false;
        }
        //    // 判断是否三消
        public static Check3Match(arr: any[] = null) {
                let keyValue_arr = [];
                for (let index = 0; index < arr.length; index++) {
                        let item = {
                                index: index,
                                value: arr[index],
                        };
                        let isHas = keyValue_arr.hasOwnProperty(item.value);
                        if (!isHas) {
                                keyValue_arr[item.value] = [];
                        }
                        keyValue_arr[item.value].push(item);;

                        // console.log(`keyValue_arr`, keyValue_arr[item.value]);
                        if (keyValue_arr[item.value].length >= 3) {
                                // console.log("3个");
                                // TipPanel.Instance.Show(`3个`);
                                // console.log(`value`, item.value);
                                return keyValue_arr[item.value];
                        }
                }

                return [];
        }
        //    // 是否全部消除
        public static CheckIsAllDelete() {
                let isAllDelete: boolean = true;

                for (let index = 0; index < YangLeGeYang_GameData.MapData_CurrentLevel.length; index++) {
                        const element = YangLeGeYang_GameData.MapData_CurrentLevel[index];
                        if (element.cardGameState !== YangLeGeYang_CardGameState.消除) {
                                isAllDelete = false;
                                break;
                        }
                }

                return isAllDelete;
        }
        //    // 洗牌
        public static WashCards() {
                let temp_array = [];
                for (let index = 0; index < YangLeGeYang_GameData.MapData_CurrentLevel.length; index++) {
                        const element = YangLeGeYang_GameData.MapData_CurrentLevel[index];
                        if (element.cardGameState === YangLeGeYang_CardGameState.游戏场景) {
                                temp_array.push(element.card_type);
                        }
                }

                // console.log(`temp_array`, temp_array);
                ArrayUtils.ShuffleArray(temp_array);
                // ArrayUtils.shuffle(temp_array);
                // console.log(`temp_array`, temp_array);

                let index_temp_array = 0;
                for (let index = 0; index < YangLeGeYang_GameData.MapData_CurrentLevel.length; index++) {
                        const itemdata_MapData_CurrentLevel = YangLeGeYang_GameData.MapData_CurrentLevel[index];
                        const card = YangLeGeYang_CardManager.Instance.CardList[index];
                        const item_temp_array = temp_array[index_temp_array];
                        if (itemdata_MapData_CurrentLevel.cardGameState === YangLeGeYang_CardGameState.游戏场景) {
                                itemdata_MapData_CurrentLevel.card_type = item_temp_array;
                                card.card_type = item_temp_array;
                                // card.Refresh();
                                index_temp_array++;
                        }
                }

                // console.log(`DanLeGeCi_GameData.MapData_CurrentLevel`, DanLeGeCi_GameData.MapData_CurrentLevel);
                // console.log(`CardManager.Instance.CardList`, CardManager.Instance.CardList);

                for (var index = 0; index <= YangLeGeYang_CardManager.Instance.CardList.length - 1; index++) {
                        // const itemdata: any = CardManager.Instance.CardList[i];
                        const itemdata_MapData_CurrentLevel = YangLeGeYang_GameData.MapData_CurrentLevel[index];

                        // console.log(itemdata_MapData_CurrentLevel.cardGameState)

                        if (itemdata_MapData_CurrentLevel.cardGameState === YangLeGeYang_CardGameState.游戏场景) {

                                let card: YangLeGeYang_Card = YangLeGeYang_CardManager.Instance.CardList[index];

                                let pos_old = card.node.position;
                                let pos = v2(0, 0);

                                tween(card.node)
                                        // .to(0.5, { x: pos.x, y: pos.y, scale: v3(0, 0, 0) })
                                        .to(0.5, { position: v3(pos.x, pos.y) })
                                        // .to(0.05, { scale: 0 })
                                        .call(() => {
                                                card.Refresh();
                                                card.RefreshIcon();
                                        })
                                        .delay(0.2)
                                        // .to(0.05, { scale: 1 })
                                        // .to(0.5, { x: pos_old.x, y: pos_old.y, scale: v3(1, 1, 1) })
                                        .to(0.5, { position: v3(pos_old.x, pos_old.y) })
                                        .call(() => {

                                        })
                                        .start();

                                tween(card.node)
                                        .to(0.5, { scale: v3(0, 0, 0) })
                                        .delay(0.2)
                                        .to(0.5, { scale: v3(1, 1, 1) })
                                        .start();
                        }
                }

                return true;
        }
        //    // 移除操作台
        public static RefreshEmptyLand() {
                for (let index = 0; index < YangLeGeYang_GameData.MapData_EmptyLand.length; index++) {
                        const itemdata = YangLeGeYang_GameData.MapData_EmptyLand[index];
                        // console.log(`itemdata-` + index, itemdata);

                        const card: YangLeGeYang_Card = YangLeGeYang_CardManager.Instance.CardList[itemdata.list_index];
                        // let width = OperatePanel.Instance.node.width - 274;
                        // const width_half: number = width / 1//2;
                        // let width_cell: number = DanLeGeCi_GameData.CellWidth;
                        // let x_start = -width_half + width_cell + width_cell * index;

                        let w = YangLeGeYang_GameData.CellWidth;
                        let x_start = -300 + w * index;
                        let y = 140;
                        let pos = v3(x_start, y);
                        card.node.setPosition(pos);
                }
        }
        //    // 移除操作台
        public static RemoveOperatePanel() {
                // return
                if (YangLeGeYang_GameData.MapData_Operate.length === 0) {
                        return false;
                }

                for (let index = 2; index >= 0; index--) {
                        const itemdata = YangLeGeYang_GameData.MapData_Operate[index];
                        // console.log(`itemdata-` + index, itemdata);

                        if (itemdata) {
                                // let index_MapData_Operate = index;
                                const card: YangLeGeYang_Card = YangLeGeYang_CardManager.Instance.CardList[itemdata.list_index];
                                YangLeGeYang_GameData.MapData_EmptyLand.push(itemdata);
                                YangLeGeYang_GameData.MapData_Operate.splice(index, 1);
                                itemdata.cardGameState = YangLeGeYang_CardGameState.空地;
                                // card.node.setPosition(0, 200);

                                // let index_MapData_Operate = index;
                                // const card: Card = CardManager.Instance.CardList[element.list_index];

                                // let worldPos = card.node.convertToWorldSpaceAR(Vec3.ZERO);
                                // card.node.setParent(OperatePanel.Instance.node);
                                // let dstPos = card.node.parent.convertToNodeSpaceAR(worldPos);
                                // card.node.setPosition(dstPos);
                        }
                }

                // console.log(`DanLeGeCi_GameData.MapData_EmptyLand`, DanLeGeCi_GameData.MapData_EmptyLand);
                // console.log(`DanLeGeCi_GameData.MapData_Operate`, DanLeGeCi_GameData.MapData_Operate);

                YangLeGeYang_GameFunction.RefreshEmptyLand();
                // for (let index = 0; index < DanLeGeCi_GameData.MapData_EmptyLand.length; index++) {
                //     const itemdata = DanLeGeCi_GameData.MapData_EmptyLand[index];
                //     // console.log(`itemdata-` + index, itemdata);

                //     const card: Card = CardManager.Instance.CardList[itemdata.list_index];
                //     let width = OperatePanel.Instance.node.width - 274;
                //     const width_half: number = width / 1//2;
                //     let width_cell: number = DanLeGeCi_GameData.CellWidth;
                //     // let x_start = -width_half + width_cell + width_cell * index;

                //     let w = 94
                //     let x_start = -300 + w * index;
                //     let y = 140;
                //     let pos = v2(x_start, y);
                //     card.node.setPosition(pos);
                // }

                YangLeGeYang_OperatePanel.Instance.RefreshAllCard();

                return true;
        }
        //    // 撤回一步
        public static Ctrl_Z() {
                if (YangLeGeYang_GameData.LastClickCardIndex < 0) {
                        return false;
                }

                let length = YangLeGeYang_GameData.MapData_Operate.length;
                if (length === 0) {
                        return false;
                }

                let card: YangLeGeYang_Card = YangLeGeYang_CardManager.Instance.CardList[YangLeGeYang_GameData.LastClickCardIndex];
                let mapData_LastClickCardIndex = YangLeGeYang_GameData.MapData_CurrentLevel[YangLeGeYang_GameData.LastClickCardIndex];

                if (mapData_LastClickCardIndex.cardGameState === YangLeGeYang_CardGameState.消除) {
                        return false;
                }

                const card_type = mapData_LastClickCardIndex.card_type;
                const card_layer = mapData_LastClickCardIndex.card_layer;
                const card_pos = mapData_LastClickCardIndex.card_pos;
                const cardParent = YangLeGeYang_LevelManager.Instance.LayerList[card_layer - 1];
                // const startPst = cardParent.convertToNodeSpaceAR(card.node.convertToWorldSpaceAR(Vec3.ZERO));

                console.log(`card_pos`, card_pos);

                const endPst = v3(card_pos.x * YangLeGeYang_GameData.CellWidth, card_pos.y * YangLeGeYang_GameData.CellHeight2);
                // card.node.parent = cardParent;
                // card.node.setPosition(startPst);
                CommonFunction.SetParent(card.node, cardParent);
                tween(card.node).to(0.3, { position: endPst }).start();
                mapData_LastClickCardIndex.cardGameState = YangLeGeYang_CardGameState.游戏场景;

                ArrayUtils.Remove(YangLeGeYang_GameData.MapData_Operate, mapData_LastClickCardIndex);

                // console.log(`Ctrl_Z------`);

                YangLeGeYang_OperatePanel.Instance.RefreshAllCard();

                YangLeGeYang_GameFunction.RefreshAllCardCover(YangLeGeYang_GameData.MapData_CurrentLevel);

                return true;
        }
}